package com.clz.hyperit.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


import com.clz.hyperit.entity.Role;
import com.clz.hyperit.service.RoleService;
import com.clz.hyperit.util.ResultMessage;


@RestController
@RequestMapping(value = "/role")
public class RoleController {
	private final Logger logger = LoggerFactory.getLogger(RoleController.class);
	private ResultMessage result = null;
	
	@Autowired
	private RoleService roleService;
	
	/**
	 * 添加角色
	 */
	@RequestMapping(value = "/addRole", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> addRole(Role role){
		logger.info("addRole");
		result = new ResultMessage();
		result.setServiceResult(true);
		boolean flag = roleService.addRole(role);
		if(flag){
			result.setResultInfo("添加角色成功");
		}
		else{
			result.setResultInfo("添加角色失败");
		}
		return new ResponseEntity<ResultMessage>(result,HttpStatus.OK);
	}
	/**
	 * 删除角色
	 */
	@RequestMapping(value = "/deleteRole", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> deleteRole(Integer roleId){
		logger.info("deleteRole");
		result = new ResultMessage();
		result.setServiceResult(true);
		roleService.deleteRole(roleId);
		result.setResultInfo("删除角色成功");
		return new ResponseEntity<ResultMessage>(result,HttpStatus.OK);
	}
	/**
	 * 修改角色
	 */
	@RequestMapping(value = "/updateRole", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> updateRole(Role role){
		logger.info("updateRole");
		result = new ResultMessage();
		result.setServiceResult(true);
		roleService.updateRole(role);
		result.setResultInfo("更新角色成功");
		return new ResponseEntity<ResultMessage>(result,HttpStatus.OK);
	}
	/**
	 * 获取所有角色
	 */
	@RequestMapping(value = "/findRoles", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> findRoles(){
		logger.info("findRoles");
		result = new ResultMessage();
		result.setServiceResult(true);
		List<Role> list = roleService.findRoles();
		result.getResultParm().put("rolelist",list);
		result.setResultInfo("查找成功");
		return new ResponseEntity<ResultMessage>(result,HttpStatus.OK);
	}
}
